package com.flick.base.dao;

import com.flick.common.Filters;
import com.flick.common.Rule;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @author flick on 2017/4/27.
 * @version 1.0
 */
public class Myspec<T> implements Specification<T> {

    private  Filters filters;


    public Myspec(Filters filters){
        this.filters = filters;
    }

    @Override
    public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        if(filters != null){
            List<Rule> rules = filters.getRules();
            String op = filters.getGroupOp();
            List<Filters> filtersList = filters.getGroups();
            Predicate predicate = null;
            if(rules != null){
                for (Rule rule:rules) {
//                    query.where(cb.like(root.get(rule.getField()),rule.getData()));
                    predicate = opToJpa(rule,root,cb);
                }
                query.where(predicate);
            }
        }
        return null;
    }

    public Predicate opToJpa(Rule rule,Root<T> root,CriteriaBuilder cb){
        String op = rule.getOp();
        String filed = rule.getField();
        String data = rule.getData();
        if(op != null) {
            switch (op) {
                case "eq":
                    //			sqlop = "=";
                    return cb.equal(root.get(filed),data);
//                    break;
                case "ne":
                    //			sqlop = "<>";
                    return cb.notEqual(root.get(filed),data);
                case "lt":
                    //			sqlop = "<";
                    return cb.lt(root.get(filed),Long.parseLong(data));
//                    break;
                case "le":
                    return cb.le(root.get(filed),Long.parseLong(data));
                    //			sqlop = "<=";
//                    break;
                case "gt":
                    return cb.gt(root.get(filed),Long.parseLong(data));
                    //			sqlop = ">";
//                    break;
                case "ge":
                    return cb.ge(root.get(filed),Long.parseLong(data));
                    //			sqlop = ">=";
//                    break;
                case "bw":
                    return cb.ge(root.get(filed),Long.parseLong(data));
                    //			sqlop = ">=";
//                    break;
                case "bn":
                    return cb.lt(root.get(filed),Long.parseLong(data));
                    //			sqlop = "<";
//                    break;
                case "in":
                    //			sqlop = "in";
                    return cb.equal(root.get(filed),data);
//                    break;
                case "ni":
                    return cb.notEqual(root.get(filed),data);
                    //			sqlop = "not in";
//                    break;
                case "ew":
                    return cb.le(root.get(filed),Long.parseLong(data));
                    //			sqlop = "<=";
//                    break;
                case "en":
                    return cb.gt(root.get(filed),Long.parseLong(data));
                    //			sqlop = ">";
//                    break;
                case "cn":
                    return cb.like(root.get(filed),"%"+data+"%");
                    //			sqlop = "like";
//                    break;
                case "nc":
                    return cb.notLike(root.get(filed),"%"+data+"%");
                    //			sqlop = "not like";
//                    break;
                default:
                    return cb.equal(root.get(filed),data);
                    //			sqlop = "=";
//                    break;
            }
        }
        return null;
    }
}
